package cn.xfdzx.android.apps.shop.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.bean.OrderListBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;

/**
 * @author gzk
 * @date 2020/4/13 0013
 * description：我的订单中内嵌商品
 */
public class ItemAllOrderGoodsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private ItemClickListener mItemClickListener;
    private List<OrderListBeen.DataBean.CartInfoBean> list;
    private Context mContext;

    /**
     * 类型1：
     */
    public static final int TYPE_ITEM = 0;
    /**
     * 类型2：
     */
    public static final int TYPE_BOTTOM = 1;
    /**
     * 当前类型
     */
    public int mCurrentType = TYPE_ITEM;
    private final LayoutInflater mLayoutInflater;

    public void setList(List<OrderListBeen.DataBean.CartInfoBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public ItemAllOrderGoodsAdapter(Context context) {
        this.mContext = context;
        //以后用它来初始化布局
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getItemViewType(int position) {
        if (position == list.size()) {
            mCurrentType = TYPE_BOTTOM;
        } else {
            mCurrentType = TYPE_ITEM;
        }
        return mCurrentType;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        if (viewType == TYPE_ITEM) {
            View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_all_order_goods, parent, false);
            return new MyViewHolder(mContext, inflate);
        } else if (viewType == TYPE_BOTTOM) {
            return new BottomTypeViewHolder(mContext, mLayoutInflater.inflate(R.layout.item_car_retails_bottom_alpha, null));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int type = getItemViewType(position);
        switch (type) {
            case TYPE_ITEM:
                ((MyViewHolder) holder).setData(position);
                break;

            case TYPE_BOTTOM:
                ((BottomTypeViewHolder) holder).setData();
                break;

        }
        holder.itemView.setTag(position);
    }


    @Override
    public int getItemCount() {
        return null == list ? 0 : list.size() + 1;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_goods)
        ImageView ivGoods;
        private Context mContext;

        public MyViewHolder(Context mContext, View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.mContext = mContext;
        }

        public void setData(int position) {
            RoundedCorners roundedCorners = new RoundedCorners(20);
            //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
            RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(200, 200);
            options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                    .fallback(R.drawable.icon_defult) //url为空的时候,显示的图片
                    .error(R.drawable.icon_defult);
//            RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
            Glide.with(mContext).load(list.get(position).getProductInfo().getImage()).apply(options.transform(new CenterCrop(), roundedCorners)).into(ivGoods);


        }
    }


    public void setOnItemClickListener(ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;

    }


    public class BottomTypeViewHolder extends RecyclerView.ViewHolder {


        private Context mContext;


        public BottomTypeViewHolder(Context mContext, View itemView) {
            super(itemView);
            this.mContext = mContext;
        }

        public void setData() {


        }
    }

}
